"use client"

import { ColumnDef } from "@tanstack/react-table"
import { Track } from "@/types/admin" 
import { Button } from "@/components/ui/button"
import { ArrowUpDown, MoreHorizontal, Pencil, Trash2, Train, AlertTriangle, CheckCircle } from "lucide-react"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import StatusBadge from "@/components/status-badge"

interface ColumnActionsProps {
  onEdit: (track: Track) => void
  onDelete: (track: Track) => void
}

export const columns = ({ onEdit, onDelete }: ColumnActionsProps): ColumnDef<Track>[] => [
  {
    accessorKey: "name",
    header: ({ column }) => {
      return (
        <Button
          variant="ghost"
          onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
          className="hover:bg-transparent"
        >
          股道名称
          <ArrowUpDown className="ml-2 h-4 w-4" />
        </Button>
      )
    },
    cell: ({ row }) => (
      <div className="font-medium flex items-center">
        <Train className="h-4 w-4 mr-2 text-primary" />
        {row.getValue("name")}
      </div>
    ),
  },
  {
    accessorKey: "code",
    header: "股道编号",
    cell: ({ row }) => (
      <div className="font-mono text-sm">{row.getValue("code")}</div>
    ),
  },
  {
    accessorKey: "type",
    header: "股道类型",
    cell: ({ row }) => (
      <StatusBadge status="default">
        {row.getValue("type")}
      </StatusBadge>
    ),
  },
  {
    accessorKey: "status",
    header: "状态",
    cell: ({ row }) => {
      const status = row.getValue("status") as string
      let statusConfig = {
        status: "normal" as "normal" | "warning" | "error",
        icon: CheckCircle
      }
      
      if (status === "占用") {
        statusConfig = { status: "error", icon: AlertTriangle }
      } else if (status === "维护中") {
        statusConfig = { status: "warning", icon: AlertTriangle }
      }
      
      return (
        <StatusBadge status={statusConfig.status} className="flex items-center">
          <statusConfig.icon className="h-3 w-3 mr-1" />
          {status}
        </StatusBadge>
      )
    },
  },
  {
    id: "actions",
    cell: ({ row }) => {
      const track = row.original

      return (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button variant="ghost" className="h-8 w-8 p-0 rounded-full hover:bg-muted">
              <span className="sr-only">打开菜单</span>
              <MoreHorizontal className="h-4 w-4" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className="rounded-lg card-shadow">
            <DropdownMenuLabel>操作</DropdownMenuLabel>
            <DropdownMenuItem 
              onClick={() => onEdit(track)}
              className="cursor-pointer flex items-center"
            >
              <Pencil className="mr-2 h-4 w-4" />
              编辑
            </DropdownMenuItem>
            <DropdownMenuItem
              className="text-destructive cursor-pointer flex items-center"
              onClick={() => onDelete(track)}
            >
              <Trash2 className="mr-2 h-4 w-4" />
              删除
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      )
    },
  },
] 